package com.xmb.wechat.view.wechat.money;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.nil.sdk.utils.DateUtils;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.bean.WechatMsgBean;
import com.xmb.wechat.definterface.HintListener;
import com.xmb.wechat.definterface.OnChooseMeListener;
import com.xmb.wechat.definterface.SimpleTextWatcher;

import java.sql.Date;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/14 17:25
 */
public class WechatFundZhuanZhangActivity extends BaseActivity {

    @BindView(R2.id.tv_tips)
    TextView  mTvTips;
    @BindView(R2.id.iv_receiver)
    ImageView mIvReceiver;
    @BindView(R2.id.tv_receiver)
    TextView  mTvReceiver;
    @BindView(R2.id.tv_received)
    TextView  mTvReceived;
    @BindView(R2.id.tv_unreceived)
    TextView  mTvUnreceived;
    @BindView(R2.id.et_money)
    EditText  mEtMoney;
    @BindView(R2.id.tv_send_time)
    TextView  mTvSendTime;
    @BindView(R2.id.tv_receive_time)
    TextView  mTvReceiveTime;
    @BindView(R2.id.btn_save)
    Button    mBtnSave;

    private WechatMsgBean     mWechatMsgBean;
    private WechatContactBean mReceiverContactBean;

    public WechatFundZhuanZhangActivity() {
        super(R.layout.activity_fund_zhuanzhang);
    }

    @Override
    protected void initView() {
        //创建一个消息对象
        mWechatMsgBean = new WechatMsgBean();
        //默认设置收款人是我自己
        mReceiverContactBean = WechatContactBean.instanceNewContactMe(this);
        WechatContactBean.setupAvatarIntoImageView(mReceiverContactBean, mIvReceiver, 12);
        mTvReceiver.setText(mReceiverContactBean.getName());
        //时间
        mTvSendTime.setText(DateUtils.getCurDateByFormat("yyyy-MM-dd HH:mm"));
        mWechatMsgBean.setMsgTime(new Date(System.currentTimeMillis()));
        mTvReceiveTime.setText(DateUtils.getCurDateByFormat("yyyy-MM-dd HH:mm"));
        mWechatMsgBean.setReceiveTime(new Date(System.currentTimeMillis()));
        //转账状态
        setZhuanZhangState(true);

        mEtMoney.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChangedFinish(String value) {
                checkMoney(value);
            }
        });
        showExemption(new HintListener() {
            @Override
            public void onConfirm() {
                //do nothing
            }

            @Override
            public void onCancel() {
                WechatFundZhuanZhangActivity.this.finish();
            }
        });
    }

    @SuppressLint("SetTextI18n")
    private void checkMoney(String value) {
        try {
            if (TextUtils.isEmpty(value)) {
                mBtnSave.setEnabled(false);
                return;
            }
            double m = Double.parseDouble(value);
            if (m < 0.01) {
                mTvTips.setVisibility(View.VISIBLE);
                mTvTips.setText("单次转账不能少于0.01元");
            } else {
                mTvTips.setVisibility(View.GONE);
            }
            if (m < 0.01) {
                mBtnSave.setEnabled(false);
                return;
            }
            mWechatMsgBean.setMoney(m);
            checkIsAllSatisfy();
        } catch (Exception e) {
            ToastUtils.showShort("金额输入有误");
        }
    }

    private void setZhuanZhangState(boolean isReceived) {
        mTvReceived.setSelected(isReceived);
        mTvUnreceived.setSelected(!isReceived);
        mWechatMsgBean.setReceiveMoney(isReceived);
    }

    @OnClick({R2.id.re_receiver, R2.id.re_send_time, R2.id.tv_received, R2.id.tv_unreceived, R2.id.re_receive_time, R2.id.btn_save})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.re_receiver) {
            showRoleChooseReceiver(this, true, true, new OnChooseMeListener() {
                @Override
                public void onMeChoose(WechatContactBean mineContactBean) {
                    checkAndInitValue(mineContactBean);
                }
            });
        } else if (id == R.id.tv_received) {
            KeyboardUtils.hideSoftInput(this);
            setZhuanZhangState(true);
        } else if (id == R.id.tv_unreceived) {
            KeyboardUtils.hideSoftInput(this);
            setZhuanZhangState(false);
        } else if (id == R.id.re_send_time) {
            KeyboardUtils.hideSoftInput(this);
            showTimeChoose(true);
        } else if (id == R.id.re_receive_time) {
            KeyboardUtils.hideSoftInput(this);
            showTimeChoose(false);
        } else if (id == R.id.btn_save) {
                    WechatFundZhuanZhangDetailActivity.start(WechatFundZhuanZhangActivity.this, mReceiverContactBean, mWechatMsgBean);
        }
    }

    private void showTimeChoose(boolean isSend) {
        @SuppressLint("SimpleDateFormat")
        TimePickerView pvTime = new TimePickerBuilder(this, (date, v) -> {//选中事件回调
            if (isSend) {
                mWechatMsgBean.setMsgTime(date);
                mTvSendTime.setText(DateUtils.getStringByFormat(date, "yyyy-MM-dd HH:mm"));
            } else {
                mWechatMsgBean.setReceiveTime(date);
                mTvReceiveTime.setText(DateUtils.getStringByFormat(date, "yyyy-MM-dd HH:mm"));
            }
        }).setType(new boolean[]{true, true, true, true, true, false})// 默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确定")//确认按钮文字
                .setTitleSize(20)//标题文字大小
                .setTitleText("时间")//标题文字
                .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(true)//是否循环滚动
                .setSubmitColor(getResources().getColor(R.color.wechat_tab_bg_green))//确定按钮文字颜色
                .setCancelColor(getResources().getColor(R.color.wechat_tab_bg_green))//取消按钮文字颜色
                .setLabel("年", "月", "日", "时", "分", "秒")//默认设置为年月日时分秒
                .isCenterLabel(true) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .build();
        pvTime.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (data == null)
                return;
            WechatContactBean result = (WechatContactBean) data.getSerializableExtra("result");
            if (requestCode == REQUEST_CODE_CHOOSE_OTHER_RECEIVE || requestCode == REQUEST_CODE_CHOOSE_NEW_ADD_RECEIVE) {
                checkAndInitValue(result);
            }
            checkIsAllSatisfy();
        }
    }

    /**
     * 检查选中角色是否可赋值
     *
     * @param mineContactBeam
     */
    private void checkAndInitValue(WechatContactBean mineContactBeam) {
        if (mineContactBeam == null)
            return;
        //赋值
        mReceiverContactBean = mineContactBeam;
        //显示界面
        WechatContactBean.setupAvatarIntoImageView(mReceiverContactBean, mIvReceiver, 12);
        mTvReceiver.setText(TextUtils.isEmpty(mReceiverContactBean.getName()) ? "" : mReceiverContactBean.getName());
        checkIsAllSatisfy();
    }

    /**
     * 检查各条件是否具备(按钮是否可置able)
     */
    private void checkIsAllSatisfy() {
        String money = mEtMoney.getText().toString().trim();
        if (TextUtils.isEmpty(money))
            return;
        String receiver = mTvReceiver.getText().toString().trim();
        if (TextUtils.isEmpty(receiver))
            return;
        String sendTime = mTvSendTime.getText().toString().trim();
        if (TextUtils.isEmpty(sendTime))
            return;
        String receiveTime = mTvSendTime.getText().toString().trim();
        if (TextUtils.isEmpty(receiveTime))
            return;
        mBtnSave.setEnabled(true);
    }
}
